package org.kamionowski.supreme.persistence.entity;

import java.io.Serializable;
import org.kamionowski.supreme.persistence.entity.enums.ServiceType;

import javax.persistence.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.sql.Timestamp;

/**
 * Created by IntelliJ IDEA.
 * User: Marcin Kamionowski
 * Date: 13.04.11
 * Time: 16:07
 */
@javax.persistence.Table(name = "search_service", schema = "public", catalog = "")
@Entity
public class Service implements Serializable{
    private Integer id;

    @javax.persistence.Column(name = "id", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Id
    @SequenceGenerator(name="serviceSeq")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    private Timestamp queuedtoscan;

    @javax.persistence.Column(name = "queuedtoscan", nullable = true, insertable = true, updatable = true, length = 29, precision = 6)
    @Basic
    public Timestamp getQueuedtoscan() {
        return queuedtoscan;
    }

    public void setQueuedtoscan(Timestamp queuedtoscan) {
        this.queuedtoscan = queuedtoscan;
    }

    private Long contentSize;

    @javax.persistence.Column(name = "content_size", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public Long getContentSize() {
        return contentSize;
    }

    public void setContentSize(Long size) {
        this.contentSize = size;
    }

    private Integer port;

    @javax.persistence.Column(name = "port", nullable = false, insertable = true, updatable = true, length = 19, precision = 0)
    @Basic
    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    private ServiceType type;

    @javax.persistence.Column(name = "type", nullable = false, insertable = true, updatable = true, length = 10, precision = 0)
    @Enumerated
    public ServiceType getType() {
        return type;
    }

    public void setType(ServiceType type) {
        this.type = type;
    }

    private Scan lastScan;

    @ManyToOne
    public
    @javax.persistence.JoinColumn(name = "lastscan", referencedColumnName = "id", nullable = false)
    Scan getLastScan() {
        return lastScan;
    }

    public void setLastScan(Scan lastScan) {
        this.lastScan = lastScan;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Service that = (Service) o;

        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (lastScan != null ? !lastScan.equals(that.lastScan) : that.lastScan != null) return false;
        if (queuedtoscan != null ? !queuedtoscan.equals(that.queuedtoscan) : that.queuedtoscan != null) return false;
        if (contentSize != null ? !contentSize.equals(that.contentSize) : that.contentSize != null) return false;
        if (type != null ? !type.equals(that.type) : that.type != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = id != null ? id.hashCode() : 0;
        result = 31 * result + (queuedtoscan != null ? queuedtoscan.hashCode() : 0);
        result = 31 * result + (contentSize != null ? contentSize.hashCode() : 0);
        result = 31 * result + (type != null ? type.hashCode() : 0);
        result = 31 * result + (lastScan != null ? lastScan.hashCode() : 0);
        return result;
    }

    private Host host;

    @ManyToOne
    public
    @javax.persistence.JoinColumn(name = "host_id", referencedColumnName = "id", nullable = false)
    Host getHost() {
        return host;
    }

    public void setHost(Host host) {
        this.host = host;
    }
    public URL generateURL(){
        try{
            return new URL(this.getType() + "://" + this.getHost() + ":" + this.getPort());
        }catch(MalformedURLException exc){
            throw new RuntimeException(exc);
        }
    }
}
